package com.vx.ratingTool.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vx.ratingTool.dao.IPlayerDAO;
import com.vx.ratingTool.entities.Player;

@Service
public class PlayerBaseManager implements IPlayerBaseManager {

	@Autowired
	private IPlayerDAO playerDAO;

	public PlayerBaseManager(IPlayerDAO playerDAO) {
		super();
		this.playerDAO = playerDAO;
	}

	public PlayerBaseManager() {
		super();
	}

	@Override
	public void addPlayer(Player player) {
		playerDAO.save(player);
	}

	@Override
	public void saveList(List<Player> players) {
		playerDAO.saveList(players);
	}

	@Override
	public void update(Player player) {
		playerDAO.update(player);
	}

	@Override
	public void massUpdate(List<Player> players) {
		playerDAO.massUpdate(players);
	}

	@Override
	public void delete(Player player) {
		playerDAO.delete(player);
	}

	@Override
	public void clearDatabase() {
		playerDAO.clearDatabase();
	}

	@Override
	public Player getById(int id) {
		return playerDAO.findById(id);
	}

	@Override
	public List<Player> getListOfPlayers() {
		return playerDAO.getListOfPlayers();
	}

	public List<Player> generatePlayers(int number_of_players) {
		List<Player> players = new ArrayList<Player>();

		// generating distributed values
		int numSamples = number_of_players;
		int[] numsToGenerate = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		double[] discreteProbabilities = new double[] { 0.02, 0.05, 0.09, 0.14, 0.2, 0.2, 0.14, 0.09, 0.05, 0.02 };
		EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);

		// creating new players with distributed skill
		int[] samples = distribution.sample(numSamples);
		Random rnd = new Random();
		for (int i = 0; i < numSamples; i++) {
			int skill = (int) ((samples[i] - 1) * 10 + (rnd.nextDouble() * 10));
			String skilltag = "Normal";
			if (skill > 95) {
				skilltag = "VeryHigh";
			} else {
				if (skill > 80) {
					skilltag = "High";
				}
			}
			Player aPlayer = new Player((i + 1), "(" + skilltag + ")Bot #" + (i + 1), skill, 1500, 5, 0);
			players.add(aPlayer);
		}
		return players;
	}
}
